package com.example.dryclean_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dryclean_admin.common.QueryPageParam;
import com.example.dryclean_admin.common.Result;
import com.example.dryclean_admin.entity.Clothprice;
import com.example.dryclean_admin.entity.User;
import com.example.dryclean_admin.service.IClothpriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijiaxin
 * @since 2023-12-26
 */
@RestController
@RequestMapping("/cp")
public class ClothpriceController {
@Autowired
public IClothpriceService iClothpriceService;

        @RequestMapping("/list")
        public Result cpList(){
                return Result.success(iClothpriceService.list());
}

    @GetMapping("/findByStyle")
    public Result findByStyle(@RequestParam String style){
        List list = iClothpriceService.lambdaQuery().eq(Clothprice::getStyle,style).list();
        return list.size()>0 ? Result.success(list) : Result.fail();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Clothprice clothprice){
        return iClothpriceService.updateById(clothprice)?Result.success():Result.fail();
    }

    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return iClothpriceService.removeById(id) ? Result.success() : Result.fail();
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Clothprice clothprice){
        return iClothpriceService.save(clothprice)?Result.success():Result.fail();
    }



    @PostMapping("/listpage")
    public Result cpListPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String style = (String)param.get("style");
        String price = (String)param.get("price");
        /*System.out.println("name==="+(String) param.get("username"));*/
        Page<Clothprice> cpage = new Page();
        cpage.setCurrent(query.getPageNum());
        cpage.setSize(query.getPageSize());

        LambdaQueryWrapper<Clothprice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(style)){
            lambdaQueryWrapper.like(Clothprice::getStyle,style);
        }
        if(StringUtils.hasLength(price)){
            lambdaQueryWrapper.like(Clothprice::getPrice,price);
        }
        IPage result = iClothpriceService.page(cpage,lambdaQueryWrapper);
        System.out.println("total==" + result.getTotal());


        return Result.success(result.getRecords(), result.getTotal());
    }
}
